package com.ziyun.erp.modules.quoteFastPrintMaterial.service.impl;

import com.utility.StringUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.quoteAttribute.entity.QuoteAttributeOptionEntity;
import com.ziyun.erp.modules.quoteAttribute.service.QuoteAttributeOptionService;
import com.ziyun.erp.modules.quoteFastPrintMaterial.dao.QuoteFastPrintMaterialDao;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.quoteFastPrintMaterial.entity.QuoteFastPrintMaterialEntity;
import com.ziyun.erp.modules.quoteFastPrintMaterial.service.QuoteFastPrintMaterialService;
import org.springframework.transaction.annotation.Transactional;


@Service("QuoteFastPrintMaterialService")
public class QuoteFastPrintMaterialServiceImpl extends ServiceImpl<QuoteFastPrintMaterialDao, QuoteFastPrintMaterialEntity> implements QuoteFastPrintMaterialService {

    @Autowired
    private  QuoteFastPrintMaterialDao quoteFastPrintMaterialDao;

    @Autowired
    private QuoteAttributeOptionService quoteAttributeOptionService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<QuoteFastPrintMaterialEntity> page = this.selectPage(
                new Query<QuoteFastPrintMaterialEntity>(params).getPage(),
                new EntityWrapper<QuoteFastPrintMaterialEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<QuoteFastPrintMaterialEntity> selectFacotoryMaterialList(Integer id){
        return quoteFastPrintMaterialDao.selectFacotoryMaterialList(id);
    }

    @Override
    public List<String> findAllMaterial() {
        return quoteFastPrintMaterialDao.findAllMaterial();
    }

    @Override
    public List<Map<String,Object>> findAllOptionName2(String optionName) {
        return quoteFastPrintMaterialDao.findAllOptionName2(optionName);
    }

    @Override
    public List<Map<String, Object>> selectDetailList(Map<String, Object> params) {
        return quoteFastPrintMaterialDao.selectDetailList(params);
    }

    @Override
    public List<Integer> selectSelectedlList(Map<String, Object> params) {
        return quoteFastPrintMaterialDao.selectSelectedlList(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateMaterial(String ids) {
        if(StringUtils.isNotBlank(ids)){
            //把ids转换回数组
            String[] idArray=ids.split(",");
            //根据optionId查询optionName,已勾选的
            QuoteAttributeOptionEntity quoteAttributeOptionEntity= quoteAttributeOptionService.selectById(idArray[0]);
            String optionName=quoteAttributeOptionEntity.getOptionName();
            //封装查询参数
            Map<String,Object> params=new HashMap<>();
            params.put("factoryId",1);
            params.put("optionName",optionName);
            List<Integer> selectedList=this.selectSelectedlList(params);
            List<Integer> addList=new ArrayList<Integer>();
            //查询出之前配置的材料
            for (int i=0;i<idArray.length;i++){
                if(!selectedList.contains(Integer.parseInt(idArray[i]))){
                    addList.add(Integer.parseInt(idArray[i]));
                }
            }
            //再重新插入数据
            for (int i=0;i<addList.size();i++){
                QuoteFastPrintMaterialEntity quoteFastPrintMaterial=new QuoteFastPrintMaterialEntity();
                quoteFastPrintMaterial.setFactoryId(1);
                quoteFastPrintMaterial.setOptionId(addList.get(i));
                quoteFastPrintMaterial.setMaterialPrice(new BigDecimal(0));
                quoteFastPrintMaterial.setCreateDate(new Date());
                quoteFastPrintMaterial.setCreateUser(UserUtils.getCurentUser().getUsername());
                quoteFastPrintMaterial.setUpdateDate(new Date());
                quoteFastPrintMaterial.setUpdateUser(UserUtils.getCurentUser().getUsername());
                quoteFastPrintMaterial.setMaterialType("shumakuaiyin");
                this.insert(quoteFastPrintMaterial);
            }
            if(addList.size()==0){
                return R.error("添加的材料已经存在！");
            }
        }
        return R.ok();
    }

    @Override
    public List<QuoteFastPrintMaterialEntity> getMateriallList(Map<String, Object> params) {
        return this.baseMapper.getMateriallList(params);
    }

}
